package com.ddwl.shop.mq;

import com.alibaba.fastjson.JSON;
import com.ddwl.common.config.DingTalkNotify;
import com.ddwl.common.constant.RabbitMqConstants;
import com.ddwl.schema.vo.funds.OrderPaymentVo;
import com.ddwl.schema.bo.shop.ShopUserRecordBo;
import com.ddwl.shop.service.ShopApproveService;
import com.ddwl.shop.service.ShopUserRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;


/**
 * 类RabbitMqListener.java的实现描述：TODO
 * @author wenbin
 */
@Component
@Slf4j
public class RabbitMqMessageReceiver {


    @Resource
    private ShopUserRecordService shopUserRecordService;
    @Resource
    private ShopApproveService shopApproveService;

    @Resource
    private DingTalkNotify dingTalkNotify;

    /**
     * 记录用户登陆门店信息
     * @param shopUserRecord
     */
    @RabbitListener(bindings ={@QueueBinding(value = @Queue(value = RabbitMqConstants.SHOP_USER_RECORD_QUENE,durable = "true"),
            exchange =@Exchange(value = RabbitMqConstants.DIST_EXCHANGE),key = RabbitMqConstants.SHOP_USER_RECORD_QUENE)})
    public void thirdPartRecord(String shopUserRecord) {
        ShopUserRecordBo shopUserRecordBo = JSON.parseObject(shopUserRecord, ShopUserRecordBo.class);
        log.info("开始记录用户登陆门店消息 userId={}",shopUserRecordBo.getUserId());
        try {
            shopUserRecordService.saveShopUserRecord(shopUserRecordBo);
        }catch (Exception e){
            log.error( "开始记录用户登陆门店消息异常 userId={}",shopUserRecordBo.getUserId(),e);
            dingTalkNotify.exceptionNotify(e);
        }
    }


    /**
     *处理帮扶基金付款消息
     * @param orderPayment
     */
    @RabbitListener(bindings ={@QueueBinding(value = @Queue(value = RabbitMqConstants.ASSIST_FUND_NOTIFY_QUEUE,durable = "true"),
            exchange =@Exchange(value = RabbitMqConstants.ORDER_EXCHANGE),key = RabbitMqConstants.ASSIST_FUND_NOTIFY_QUEUE)})
    public void payShopSettledNotify(String orderPayment) {
        OrderPaymentVo orderPaymentVo = JSON.parseObject(orderPayment, OrderPaymentVo.class);
        log.info("开始处理处理帮扶基金付款消息 orderPaymentVo={}",JSON.toJSONString(orderPaymentVo));
        try {
            shopApproveService.orderPayCallBack(orderPaymentVo);
        }catch (Exception e){
            log.error( "记录处理帮扶基金付款消息 orderPaymentVo={}"+JSON.toJSONString(orderPaymentVo),e);
            dingTalkNotify.exceptionNotify(e);
        }
    }


}
